package bean;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;

public class QuestionDAO implements Utf8_{

    DataSource ds;

    public QuestionDAO() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/xmart?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("connectionTimeout", "1000"); // 连接超时：1秒
        config.addDataSourceProperty("idleTimeout", "60000"); // 空闲超时：60秒
        config.addDataSourceProperty("maximumPoolSize", "800"); // 最大连接数：200
        ds = new HikariDataSource(config);
    }

//    public ArrayList<Question> getNearest(){
//        String sql = "select * from question where id";
//        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
//            ps.setString(1, transform(question.getTitle()));
//            ps.setString(2, transform(question.getQuestionDetails()));
//            ps.setInt(3, question.getSponsor_id());
//            ps.setString(4, null);
//            Long time = System.currentTimeMillis();
//            ps.setInt(5, time.intValue());
//            ps.execute();
//            ResultSet rs = ps.executeQuery("SELECT LAST_INSERT_ID()");
//            if (rs.next()) {
//                return rs.getInt(1);
//            }
//        } catch (SQLException | UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        return -1;
//    }

    public int add(Question question) {
        String sql = "insert into question values (null,?,?,?,?,?)";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.setString(1, transform(question.getTitle()));
            ps.setString(2, transform(question.getQuestionDetails()));
            ps.setInt(3, question.getSponsor_id());
            ps.setString(4, null);
            Long time = System.currentTimeMillis();
            ps.setInt(5, time.intValue());
            ps.execute();
            ResultSet rs = ps.executeQuery("SELECT LAST_INSERT_ID()");
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void delete(int id) {
        String sql = "delete from question where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//
//        public static void main(String[] args) {
//         QuestionDAO as =new QuestionDAO();
//        Question account = as.get(1);
//        System.out.println(account.getQuestionID());
//
//    }
    public Question get(int id) {
        Question question = null;
        String sql = "select * from question where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                question = new Question();
                question.setQuestionID(rs.getInt("id"));
                question.setTitle(rs.getString("title"));
                question.setQuestionDetails(rs.getString("content"));
                question.setSponsor_id(rs.getInt("sponsor_id"));
                if(rs.getString("answerList")!=null){
                    ArrayList<String> arrayList = new ArrayList<>();
                    String[] s = rs.getString("answerList").split(",");
                    arrayList.addAll(Arrays.asList(s));
                    question.setAnswerList(arrayList);
                }

                question.setTimeStamp(rs.getInt("timestamp"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return question;
    }


}
